home *** CD-ROM | disk | FTP | other *** search
/ Aminet 43 / Aminet 43 (2001)(GTI - Schatztruhe)[!][Jun 2001].iso / Aminet / comm / tcp / rxsocket.lha / rxsocket / examples / ups.rexx < prev    next >
OS/2 REXX Batch file  |  2001-03-01  |  3KB  |  117 lines

  1. /* ups.rexx - a very stupid udp ports scanner (but it works...:-) */
  2.  
  3. l="rmh.library";if ~show("L",l) then;if ~addlib(l,0,-30) then exit
  4. if AddLibrary("rexxsupport.library","rxsocket.library","rxlibnet.library")~=0 then exit
  5.  
  6. prg=ProgramName("NOEXT")
  7.  
  8. if ~RMH_ReadArgs("HOST/A,FROM/N,TO/N,T=TIMEOUT/K/N,I=INTERFACE/K") then do
  9.     call PrintFault()
  10.     exit
  11. end
  12.  
  13. if parm.1.flag then from=parm.1.value
  14. else from=1
  15.  
  16. if parm.2.flag then high=parm.2.value
  17. else
  18.     if parm.1.flag then high=from
  19.     else high=100
  20. if from<1 | high>65535 | from>high then call err "bad ports sequence" from"..."high,1
  21.  
  22. if parm.3.flag then secs=parm.3.value
  23. else secs=5
  24. if secs<1 then call err "bad timeout value '"secs"'",1
  25.  
  26. if parm.4.flag then dev=parm.4.value
  27. else do
  28.     dev=suitable()
  29.     if dev="" then call err "no suitable device found",1
  30. end
  31.  
  32. sin.addrAddr=resolve(parm.0.value)
  33. if sin.addrAddr=-1 then call err "host <"parm.0.value"> not found",1
  34. cxaddr=c2x(addr2c(sin.addrAddr))
  35.  
  36. us=socket(inet,dgram)
  37. if us<0 then call err "can't create socket"
  38.  
  39. fstring="icmp[0]=3"
  40. filter=MiamiPCapCompile(fstring,dev)
  41. if filter="" then call err PCAPERR,1
  42.  
  43. s=2**AllocSignal()
  44. say dev s
  45. pf=MiamiCreatePF(dev,s)
  46.  
  47. if IsDotAddr(parm.0.value)
  48.     then say "Scanning <"parm.0.value"> on '"dev"'" from"..."high
  49.     else say "Scanning <"parm.0.value"> ["sin.addrAddr"] on '"dev"'" from"..."high
  50.  
  51. todo=0
  52. do i=from to high
  53.     sin.addrport=i
  54.     if sendto(us,"D0A"x,,"SIN")<0 then ports.i=0
  55.     else ports.i=1
  56.     todo=todo+ports.i
  57.     if and(SetSignal(0,s),s)>0 then do
  58.         p=MiamiPFNext(pf)
  59.         do while p~=""
  60.             if MiamiPCapMatch(filter,p) then do
  61.                 t=c2d(substr(p,21,1))
  62.                 c=c2d(substr(p,22,1))
  63. /*                addr=substr(p,45,4)
  64.                 if cxaddr~=c2x(addr) then say cxaddr c2x(addr)*/
  65.                 if t=3 & c~=3 then call err "error on icmp packet type:"t "code:"c,1
  66.                 pp=c2d(substr(p,51,2))
  67.                 ports.pp=0
  68.                 todo=todo-1
  69.             end
  70.             p=MiamiPFNext(pf)
  71.         end
  72.     end
  73. end
  74.  
  75. secs=todo%3
  76. if secs<5 then secs=5
  77. say "Sending completed, waiting for" todo "returns for" secs "seconds"
  78.  
  79. tim=CreateTimer()
  80. ts=TimerSignal(tim)
  81. call StartTimer(tim,secs)
  82. sigs=or(s,ts,2**12)
  83.  
  84. do while todo>0
  85.     res=wait(sigs)
  86.     if and(res,s)>0 then do
  87.         p=MiamiPFNext(pf)
  88.         do while p~=""
  89.             if MiamiPCapMatch(filter,p) then do
  90.                 pp=c2d(substr(p,51,2))
  91.                 ports.pp=0
  92.                 todo=todo-1
  93.             end
  94.             p=MiamiPFNext(pf)
  95.         end
  96.     end
  97.     else leave
  98. end
  99.  
  100. do i=from to high
  101.     if ports.i then say i
  102. end
  103. exit
  104.  
  105. err: procedure expose prg
  106. parse arg msg,ntdoerr
  107.     if ntdoerr~=1 then msg=msg "("ErrorString()")"
  108.     say prg":" msg
  109.     exit
  110.  
  111. suitable: procedure
  112.     res=QueryInterfaces("IN")
  113.     do i=0 to res-1
  114.         if in.i.family=2 & in.i.up=1 & in.i.LoopBack=0 then return in.i.name
  115.     end
  116.     return ""
  117.